package org.darkforest.common.services.impl;

import java.math.BigDecimal;
import java.util.List;

import org.darkforest.common.services.MobileMsgService;
import org.darkforest.common.services.PdRechargeService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.PdLogMapper;
import org.darkforest.dao.mapper.PdRechargeMapper;
import org.darkforest.model.*;
import org.darkforest.ucenter.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * 项目名称         :bwmall-ucenter
 * 文件名           :PdRechargeServiceImpl.java
 * 开发公司及作者    :陈超
 * 版本号及开发日期  :Rev. 1.00 (2015年2月15日)
 */
@Service
public class PdRechargeServiceImpl implements PdRechargeService {
	
	
	@Autowired
	private PdRechargeMapper pdRechargeMapper;

	@Autowired
	@Qualifier("wapMemberServiceImpl")
	private MemberService memberService;

	@Autowired
	private PdLogMapper pdLogMapper;

	@Autowired
	private MobileMsgService mobileMsgService;


	@Override
	public List<PdRechargeVo> findListByMenber(PdRechargeSearchBean page) {
		// TODO Auto-generated method stub
		return pdRechargeMapper.selectByMember(page);
	}

	@Override
	public PdRecharge selectByPrimaryKey(Integer pdrId) {
		return pdRechargeMapper.selectByPrimaryKey(pdrId);
	}

	@Override
	@Transactional
	public void rechargeCallBack(PdRecharge pdRecharge, String transaction_id, String pay_order_sn) {

			//修改充值表支付状态
			pdRecharge.setPdrPaymentCode(transaction_id);
			pdRecharge.setPdrPaymentState("1");
			pdRecharge.setPdrPaymentTime(DateUtil.unixTimeStampInt().intValue());
			pdRechargeMapper.updateByPrimaryKeySelective(pdRecharge);


			//用户更新余额
			MemberWithBLOBs member = memberService.findMemberById(pdRecharge.getPdrMemberId());

			//新增用户账户变动记录日志
			PdLog pdLog = new PdLog();
			pdLog.setLgDesc("用户充值 充值单号" + pdRecharge.getPdrSn());
			pdLog.setLgAddTime(DateUtil.unixTimeStampInt().intValue());
			pdLog.setLgType("recharge");
			pdLog.setLgAvAmount(pdRecharge.getPdrAmount());
			pdLog.setLgFreezeAmount(new BigDecimal(0.00));
			pdLog.setLgOrderId(pdRecharge.getPdrId());
			pdLog.setLgMemberId(member.getMemberId());
			pdLog.setLgMemberName(member.getMemberName());
			pdLog.setLgBalance(member.getAvailablePredeposit().add(pdRecharge.getPdrAmount()));
			pdLogMapper.insertSelective(pdLog);



			//更新账户余额数据
			member.setAvailablePredeposit(member.getAvailablePredeposit().add(pdRecharge.getPdrAmount()));
			memberService.updateMember(member);



	}
}
